<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>格式化数字</title>
</head>

<body>

  <form name="form">
    请输入要格式化的数字：<br />
    <input type="text" name="numberBer"><br />
    请输入要格式化的长度：<br />
    <input type="text" name="numberLen"><br />
    格式化后的数字：<br />
    <input type="text" name="numberAft"><br />
    <input type="button" onclick="deal();" id="change" value="转化">
    <input type="reset" value="重置" />
  </form>

  <script>
    function deal() {
      var ber = document.form.numberBer.value;
      var len = document.form.numberLen.value;
      if (!(ber && len)) {
        alert("输入不合法");
        return 0;
      }
      var bl = ber.length;
      for (let i = 0; i < len - bl; i++) {
        ber = '0' + ber;
      }
      document.form.numberAft.value = ber;
    }
  </script>
</body>

</html>